/**
 * Main UI. Holds all the main buttons for gameplay
 */
package View;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;


import Model.CaptainsLog;

/**
 *
 * This class is the player's main controller screen. 
 * It allows the player to go to market,
 * dockbay, warp, save, exit, or load. It also allows the player 
 * to check their ship and player stats.
 * The main player screen also displays the captain's log.
 * @author Whitney
 *@version 1.0
 *
 */
public class PlayerWindow extends JPanel {

	/**
	 * Planet image
	 */
	private final ImageIcon planet;

	/**
	 * Side Panel Dimension
	 */
	private final Dimension sidePanelDimension = new Dimension(115, 100);

	/**
	 * Main panel Dimension
	 */
	private final Dimension topPanelDimension = new Dimension(400, 300);

	/**
	 * Initial button pressed value
	 */
	private int buttonPressed = -1;

	/**
	 * All JButtons for Panel
	 */
	private final JButton btnShip, btnPlayer, btnSave, btnExit, btnWarp, 
		btnToDockbay, btnToMarketplace;

	/**
	 * Initial Text for Captain's Log
	 */
	private static final String CAPTAINSTEXT = "";

	/**
	 * Space for Captain's Log
	 */
	private final JTextArea captainsLog;

	/**
	 * Initial ship's fuel
	 */
	private int shipFuel = -1;

	/**
	 * Captain's log object
	 */
	private final CaptainsLog log;

	/**
	 * This method builds the player window. It 
	 * includes buttons for warp, market, dockbay, playerinfo,
	 * shipinfo, save, load, and exit. It also includes a 
	 * captain's log printout
	 * @param shipFuel the integer containing the current shipfuel
	 * @param techLevel the tech level of the planet will determine 
	 * 			the planet picture (0 = in space);
	 * @param fuelCapacity the amount of fuel the ship holds
	 * @param log The captain's log!
	 */
	public PlayerWindow(int shipFuel, int fuelCapacity, int techLevel, CaptainsLog log) {
		this.log = log;
		this.shipFuel = shipFuel;
		setLayout(new BorderLayout(0, 0));
		planet = new ImageIcon ("images/PlanetView/" + techLevel + ".png");
		
		final ButtonListener listen = new ButtonListener();
		
		final JPanel topPanel = new JPanel();
		topPanel.setBackground(Color.GRAY);
		topPanel.setPreferredSize(topPanelDimension);
		add(topPanel, BorderLayout.NORTH);
		
		final JLabel viewWindow = new JLabel("", planet, SwingConstants.CENTER);
		topPanel.add(viewWindow);
		
		final JPanel leftPanel = new JPanel();
		leftPanel.setBackground(Color.LIGHT_GRAY);
		leftPanel.setPreferredSize(sidePanelDimension);
		add(leftPanel, BorderLayout.WEST);
		leftPanel.setLayout(null);
		
		btnShip = new JButton("Ship");
		btnShip.setBounds(16, 68, 75, 29);
		leftPanel.add(btnShip);
		
		btnPlayer = new JButton("Character");
		btnPlayer.setBounds(6, 37, 95, 29);
		leftPanel.add(btnPlayer);
		
		final JLabel fuelLabel = new JLabel("Fuel: " + shipFuel + "/" + fuelCapacity);
		final JLabel warningLabel = new JLabel("");
		warningLabel.setBounds(10, 115, 150, 20);
		fuelLabel.setBounds(10, 99, 150, 20);
		if(((fuelCapacity > 20) && shipFuel < 10) || shipFuel < (fuelCapacity >> 1  )){
			fuelLabel.setForeground(Color.RED);
			warningLabel.setForeground(Color.RED);
			warningLabel.setText("Warning: Refuel");
		}
		else{
			fuelLabel.setForeground(Color.BLACK);
			warningLabel.setText("");
		}
		fuelLabel.setFont(new Font("Krungthep", Font.PLAIN, 15));
		warningLabel.setFont(new Font("Krungthep", Font.PLAIN, 10));
		leftPanel.add(fuelLabel);
		leftPanel.add(warningLabel);

		final JPanel bottomPanel = new JPanel();
		bottomPanel.setBackground(Color.GRAY);
		add(bottomPanel, BorderLayout.SOUTH);
		
		final JPanel rightPanel = new JPanel();
		rightPanel.setBackground(Color.LIGHT_GRAY);
		add(rightPanel, BorderLayout.EAST);
		rightPanel.setPreferredSize(sidePanelDimension);
		rightPanel.setLayout(null);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(21, 25, 75, 29);
		btnSave.addActionListener(listen);
		rightPanel.add(btnSave);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(21, 85, 75, 29);
		btnExit.addActionListener(listen);
		rightPanel.add(btnExit);
		
		final JPanel centerPanel = new JPanel();
		centerPanel.setBackground(Color.LIGHT_GRAY);
		add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(null);
		
		btnWarp = new JButton("Warp");
		btnWarp.setBounds(231, 20, 117, 29);
		btnWarp.addActionListener(listen);
		centerPanel.add(btnWarp);
		
		btnToMarketplace = new JButton("To Marketplace");
		btnToMarketplace.setBounds(218, 56, 130, 29);
		btnToMarketplace.addActionListener(listen);
		centerPanel.add(btnToMarketplace);
		
		btnToDockbay = new JButton("To Dockbay");
		btnToDockbay.setBounds(231, 90, 117, 29);
		btnToDockbay.addActionListener(listen);
		centerPanel.add(btnToDockbay);
		
		captainsLog = new JTextArea();
		captainsLog.setEditable(false);
						
		//display text in CaptainsLog text box			
		captainsLog.setText(log.toString());
		System.out.println("Log size: " + log.getEntries().size());

		captainsLog.setForeground(new Color(124, 252, 0));
		captainsLog.setFont(new Font("Courier", Font.PLAIN, 9));
		captainsLog.setBackground(new Color(0, 100, 0));
		captainsLog.setBounds(6, 6, 188, 126);
		centerPanel.add(captainsLog);
	
	}
	
	/**
	 * This class handles all buttons on the player window
	 * -1 = no buttons pressed
	 *  1 = Ship
	 *  2 = Player
	 *  3 = Exit
	 *  4 = Save
	 *  5 = Warp
	 *  6 = to Dockbay
	 *  7 = to Marketplave
	 *  8 = Bank
	 *  @author Whitney
	 */
	private class ButtonListener implements ActionListener{
		/**
		 * Handels all button events.
		 * @param event THe button events.
		 */
		public void actionPerformed (ActionEvent event){
			if(event.getSource().equals(btnShip)){
				buttonPressed = 1;
			}
			if(event.getSource().equals(btnPlayer)){
				buttonPressed = 2;
			}
			if(event.getSource().equals(btnExit)){
				buttonPressed = 3;
			}
			if(event.getSource().equals(btnSave)){
				buttonPressed = 4;
			}
			if(event.getSource().equals(btnWarp)){
				buttonPressed = 5;
			}
			if(event.getSource().equals(btnToDockbay)){
				buttonPressed = 6;
			}
			if(event.getSource().equals(btnToMarketplace)){
				buttonPressed = 7;
			}
		}

		/**
		 * The toString for the ButtonListener
		 * @return Returns a space because the button 
		 * listener doesn't need a toString. 
		 * */
		public String toString(){
			return "";
			
		}
	}
	
	/**
	 * This function returns the value of the button on the 
	 * player window that's been pressed
	 * -1 = no buttons pressed
	 *  1 = Ship
	 *  2 = Player
	 *  3 = Exit
	 *  4 = Save
	 *  5 = Warp
	 *  6 = to Dockbay
	 *  7 = to Marketplace
	 * @return the integer value representing the pressed 
	 * 			button. -1 if no button has been presssed
	 */
	public int getButtonPressed(){
		return buttonPressed;
	}
	
	/**
	 * This method adds new text to the captain's log display screen
	 * Concatenates the new string to the top of old captain's 
	 * log text and includes a "/n"
	 * so that the most current information is displayed on top
	 * @param newText a new string to be added to the captain's log
	 */
	public void setCaptainsLog(String newText){
		captainsLog.setText(newText + "/n" + CAPTAINSTEXT);
	}
	
	/**
	 * Sets market interactions
	 * @param s The string!
	 */
	public void setMarketInteractions(String s){
		log.publish(s);
	}
		
}